/**
 * COPYRIGHT (C) 2009. Denny Tran, Omar Jaime, David Deng, Julie Hsieh. All Rights Reserved
 * The main frame of the program which displays all the options available to the user.
 * @author Denny Tran, Omar Jaime, David Deng, Julie Hsieh 
 * @version 1.01 2009-05-05
 */

package gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.sql.SQLException;

import javax.swing.*;

import data.GroupSystem;
import data.User;

public class HomeFrame extends JFrame
{
    private ViewManager viewManager;
    private JPanel menuPanel;
    private JPanel viewPanel;
    private final static int FRAME_WIDTH = 1000;
    private final static int FRAME_HEIGHT = 700;
    
    /**
     * Constructs a frame with options for the user.
     * @param viewManager The viewManager to connect the HomeFrame to.
     */
    public HomeFrame(final ViewManager viewManager)
    {
        // View Panel for changing views
        viewPanel = new JPanel();
        viewPanel.setLayout(null);
        viewPanel.setLocation(200, 73);
        viewPanel.setSize(750,650);
        
        this.add(viewPanel);
        
        // view manager uses view panel so add that first
        this.viewManager = viewManager;
        
        this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        this.setLayout(null);
        
        JLabel title = new JLabel("Mojito Group Scheduling System");
        title.setFont(new Font("Serif", Font.BOLD, 30));
        title.setForeground(new Color(0,150,0));
        title.setSize(500,50);
        title.setLocation(350,20);
        this.add(title);
        
        // menu panel
        menuPanel = new JPanel();
        menuPanel.setLocation(30,75);
        menuPanel.setSize(125, 235);
        menuPanel.setLayout(null);
        
        //images
        URL imageURL = HomeFrame.class.getResource("mojito.jpg");
        ImageIcon mojito = new ImageIcon(imageURL);
        JButton mojitoBtn = new JButton(mojito);
        mojitoBtn.setSize(100, 75);
        mojitoBtn.setLocation(35,35);
        this.add(mojitoBtn);
        
        JLabel userNameLabel = new JLabel("User: "+viewManager.getGs().getUser().getUserName());
        userNameLabel.setSize(150,30);
        userNameLabel.setLocation(30, 40);
        menuPanel.add(userNameLabel);
        
        // size and location settings
        int buttonY = 75;
        int buttonX = 5;
        int buttonSpread = 25;
        Dimension buttonSize = new Dimension(100,20);
        
        JButton calendarButton = new JButton("Calendar");
        calendarButton.setLocation(buttonX, buttonY);
        buttonY+=buttonSpread;
        calendarButton.setSize(buttonSize);
        calendarButton.addActionListener(viewManager.addLink("calendar"));
        menuPanel.add(calendarButton);
        
        JButton documentButton = new JButton("Document");
        documentButton.setLocation(buttonX, buttonY);
        buttonY+=buttonSpread;
        documentButton.setSize(buttonSize);
        documentButton.addActionListener(viewManager.addLink("document"));
        menuPanel.add(documentButton);
        
        JButton editProfileButton = new JButton("Edit Profile");
        editProfileButton.setLocation(buttonX, buttonY);
        buttonY+=buttonSpread;
        editProfileButton.setSize(buttonSize);
        editProfileButton.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e)
            {
               User u = viewManager.getGs().getUser();
               EditProfile view = (EditProfile)viewManager.getView("edit profile");
               viewManager.showView("edit profile");
               view.setView(u);
            }
        });
        menuPanel.add(editProfileButton);
        
        if (viewManager.getGs().getUser().isAdmin())
        {
            JButton editGroupButton = new JButton("Edit Group");
            editGroupButton.setLocation(buttonX, buttonY);
            buttonY+=buttonSpread;
            editGroupButton.setSize(buttonSize);
            editGroupButton.addActionListener(viewManager.addLink("edit group"));
            menuPanel.add(editGroupButton);
        }
        
        JButton emailButton = new JButton("Email");
        emailButton.setLocation(buttonX, buttonY);
        buttonY+=buttonSpread;
        emailButton.setSize(buttonSize);
        emailButton.addActionListener(viewManager.addLink("email"));
        menuPanel.add(emailButton);
        
        JButton logoutButton = new JButton("Logout");
        logoutButton.setLocation(buttonX, buttonY);
        buttonY+=buttonSpread;
        logoutButton.setSize(buttonSize);
        logoutButton.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent arg0)
            {
               dispose();
               
               try
               {
                  viewManager.getGs().login();
               } 
               catch (SQLException e)
               {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }
               
               //viewManager.getGs().fakelogin();
            }
           
        });
        menuPanel.add(logoutButton);
        
        
        this.add(menuPanel);
        
        this.setTitle("Mojito");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    
    /**
     * Returns the view panel of the home frame.
     * @return The view panel of the home frame.
     */
    public JPanel getViewPanel()
    {
        return viewPanel;
    }
}
